package com.earnings.okhttputils.utils.god;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Window;
import android.view.WindowManager;

import com.earnings.okhttputils.utils.ui.dialog.ProgressDialog;
import com.earnings.okhttputils.utils.utils.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoLayoutActivity;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public abstract class GodBaseActivity extends AutoLayoutActivity {
    protected Intent mIntent;
    protected Bundle bundleData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIntent = getIntent();
        bundleData = getIntent().getExtras();
        if (bundleData == null) bundleData = new Bundle();
        onGodCreate(savedInstanceState);
        init();

    }

    protected abstract void onGodCreate(Bundle savedInstanceState);

    protected abstract void init();

    protected void skipActivity(Class cls) {
        if (mIntent == null) mIntent = new Intent();
        mIntent.setClass(this, cls);
        mIntent.putExtras(bundleData);
        startActivity(mIntent);
    }

    protected void skipActivity(Class cls, int ResultCode) {
        if (mIntent == null) mIntent = new Intent();
        mIntent.setClass(this, cls);
        mIntent.putExtras(bundleData);
        startActivityForResult(mIntent, ResultCode);
    }

    protected void skipActivityFinish(Class cls) {
        if (mIntent == null) mIntent = new Intent();
        mIntent.setClass(this, cls);
        mIntent.putExtras(bundleData);
        startActivity(mIntent);
        finish();
    }

    protected void skipActivityFinish(Class cls, int ResultCode) {
        if (mIntent == null) mIntent = new Intent();
        mIntent.setClass(this, cls);
        mIntent.putExtras(bundleData);
        startActivityForResult(mIntent, ResultCode);
        finish();
    }

    ProgressDialog progressDialog;

    protected void showProgress() {
        if (progressDialog == null) progressDialog = new ProgressDialog(this);
        if (progressDialog.isShowing()) return;
        progressDialog.show();
    }

    protected void showProgress(String text) {
        if (progressDialog == null) progressDialog = new ProgressDialog(this);
        progressDialog.setText(text);
        progressDialog.show();
    }

    protected void dismissProgress() {
        if (progressDialog == null) return;
        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }


    /**
     * 状态栏设置
     *
     * @param color
     */
    protected void settingStatusBar(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            setTranslucentStatus(true);
            //    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            // 激活状态栏设置
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(color);
            SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            //After LOLLIPOP not translucent status bar
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //Then call setStatusBarColor.
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(color));
        }
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
